package com.lg.erp.controller.zhanghuan;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lg.erp.entity.JsonResult;
import com.lg.erp.entity.zhanghuan.TDriver;
import com.lg.erp.entity.zhanghuan.TLogistics;
import com.lg.erp.entity.zhanghuan.TVehicle;
import com.lg.erp.service.zhanghuan.TDriverService;
import com.lg.erp.service.zhanghuan.TLogisticsService;
import com.lg.erp.service.zhanghuan.TVehicleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;


/***
 *
 * 车辆信息
 * */
@RestController
public class TVehicleController {

    @Autowired
    private TVehicleService tVehicleService;

    @Autowired
    private TLogisticsService TLogisticsService;
    @Autowired
    private TDriverService tDriverService;


    @GetMapping("/vehicles")
    public JsonResult pageTVehicle(@RequestParam(value = "current", required = false, defaultValue = "1") Integer current,String vehicleName,String numberPlate) {

        Page<TVehicle> page = new Page<TVehicle>(current, 5);
        IPage<TVehicle> tVehicleIPage = tVehicleService.pageTVehicle(page, vehicleName, numberPlate);
        return new JsonResult(1, "ok", tVehicleIPage);

    }

    //查询所有物流公司
    @GetMapping("/logisticsNames")
    public JsonResult AllLogistics() {
        List<TLogistics> list = TLogisticsService.list();
        return new JsonResult(1, "ok", list);
    }

    //查询驾驶员
    @GetMapping("/driversNames")
    public JsonResult AllDrivers() {
        List<TDriver> list = tDriverService.list();
        return new JsonResult(1, "ok", list);
    }

    @PostMapping("/vehicle")
    public JsonResult addTVehicle(@RequestBody TVehicle tVehicle) {
        String id = UUID.randomUUID().toString().replace("-", "").substring(0, 10);
        tVehicle.setVehicleId(id);
        tVehicle.setDriverCapacity("20");
        tVehicle.setStatus("有任务");
        LocalDateTime dateTime = LocalDateTime.now();
        tVehicle.setCreateTime(dateTime);
        tVehicleService.save(tVehicle);
        return new JsonResult(1, "ok", null);

    }

    @PutMapping("/vehicle")
    public JsonResult updateTVehicle(@RequestBody TVehicle tVehicle) {
        LocalDateTime dateTime = LocalDateTime.now();
        tVehicle.setUpdateTime(dateTime);
        QueryWrapper<TVehicle> wrapper = new QueryWrapper<>();
        wrapper.eq("vehicle_id", tVehicle.getVehicleId());
        tVehicleService.update(tVehicle, wrapper);
        return new JsonResult(1, "ok", null);

    }


    @DeleteMapping("/vehicle/{id}")
    public JsonResult deleteTVehicle(@PathVariable String id) {
        System.out.println("删除：" + id);
        tVehicleService.remove(new QueryWrapper<TVehicle>().eq("vehicle_id", id));
        return new JsonResult(1, "ok", null);
    }




}
